angular.module('starter.services', [])

.factory 'Chats', ->
    # Might use a resource here that returns a JSON array

    # Some fake testing data
    chats = [{
            id: 0,
            name: 'Ben Sparrow',
            lastText: 'You on your way?',
            face: 'https://pbs.twimg.com/profile_images/514549811765211136/9SgAuHeY.png'
        }, {
            id: 1,
            name: 'Max Lynx',
            lastText: 'Hey, it\'s me',
            face: 'https://avatars3.githubusercontent.com/u/11214?v=3&s=460'
        }, {
            id: 2,
            name: 'Andrew Jostlin',
            lastText: 'Did you get the ice cream?',
            face: 'https://pbs.twimg.com/profile_images/491274378181488640/Tti0fFVJ.jpeg'
        }, {
            id: 3,
            name: 'Adam Bradleyson',
            lastText: 'I should buy a boat',
            face: 'https://pbs.twimg.com/profile_images/479090794058379264/84TKj_qa.jpeg'
        }, {
            id: 4,
            name: 'Perry Governor',
            lastText: 'Look at my mukluks!',
            face: 'https://pbs.twimg.com/profile_images/491995398135767040/ie2Z_V6e.jpeg'
    }];

    {
        all: ->
            chats
        remove: (chat) ->
            chats.splice(chats.indexOf(chat), 1)
        get: (chatId) ->
            chats.forEach (chat) ->
                if chat.id == parseInt(chatId) then return chat

                # 等效于
                # if chat.id == parseInt(chatId)
                #     return chat
            # chats.forEach (chat) -> 也等效于
            # _.find chats, (chat) ->
            #
            # chats.forEach 是用coffee针对数组的forEach扩展函数
            # _.find 是underscore实现的函数
            null
    }
